<?php

namespace app\mobile\controller;

use app\admin\model\Users;
use app\common\traits\Gate;
use app\common\traits\Wechat;
use think\Controller;
use think\Db;
use app\common\model\Order;
use think\helper\Time;
use app\common\model\Leibie;
use app\common\common;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/10
 * Time: 10:54
 */
class Jianbao extends Controller
{
    use Gate, Wechat;
    public $userid = '';

    public function _initialize()
    {
        $login = request()->has('state');
        if (!$login) {
            $url = request()->controller() . DIRECTORY_SEPARATOR . request()->action();
            self::getInstance()->oauth->redirect(url($url, '', true, true))->send();
        } else {
            $oauth = self::getInstance()->oauth;//获取微信回掉回来的东西
            $user = $oauth->user()->toArray();
            foreach ($user['original'] as $key => $value) {
                $user[$key] = $value;
            }
            unset($user['original']);
            unset($user['token']);
            $data = Users::get(['openid' => $user['id']]);
            if ($this->hasRole('jianbao', $data->id)) {
                $this->userid = $data->id;
            } else {
                $this->redirect('autherror/autherror');
            }
        }

//        $this->userid = 25;

    }

    //每日简报
    public function getDayData()
    {
        $type = 1;
        $time = Time::today();
        $order = new Order();
        if(  $this->hasRole('yaoyuezhuanyuan',$this->userid ) ){
            $data = $order->zhuanyuan_Jianbao($time,$this->userid,$type);
            $mbdata = false;
            $jingli = 3;
        } elseif( $this->hasRole("yaoyuezhuguan",$this->userid )  ){
            $yg = Db::name('users')->where(['status'=>1,'id'=>$this->userid])->field('ygroup_id')->find();
            $ygroup = $yg['ygroup_id'];
            $data = $order->zhuguan_Jianbao($time,$ygroup,$type);
            $mbdata = $this->get_Zhuguan_Wcl_Data($data,$type,$ygroup);
            $jingli = 2;
        }else{
            $data = $order->jingli_Jianbao($time,$type);
            $jingli = 1;
            $mbdata = $this->get_Common_Data($data,$type);
        }
        return view("jianbao", ['title' => '今日', 'data' => $data, 'mbdata' => $mbdata,'type'=>$type,'jingli'=>$jingli]);
    }


    //每月简报
    public function getYueData()
    {
        $type = 2;
        $time = Time::month();
        $order = new Order();
        if(  $this->hasRole('yaoyuezhuanyuan',$this->userid ) ){
            $data = $order->zhuanyuan_Jianbao($time,$this->userid,$type);
            $mbdata = false;
            $jingli = 3;
        } elseif( $this->hasRole("yaoyuezhuguan",$this->userid )  ){
            $yg = Db::name('users')->where(['status'=>1,'id'=>$this->userid])->field('ygroup_id')->find();
            $ygroup = $yg['ygroup_id'];
            $data = $order->zhuguan_Jianbao($time,$ygroup,$type);
            $mbdata = $this->get_Zhuguan_Wcl_Data($data,$type,$ygroup);
            $jingli = 2;
        }else{
            $data = $order->jingli_Jianbao($time,$type);
            $jingli = 1;
            $mbdata = $this->get_Common_Data($data,$type);
        }
        return view("jianbao", ['title' => '当月', 'data' => $data, 'mbdata' => $mbdata,'type'=>$type,'jingli'=>$jingli]);
    }


    public function get_Zhuguan_Wcl_Data($data,$type,$ygroup){
        //成功拍摄
        $mbdatas = Db::name('renwus')->where(['status' => 1, 'date' => date('Y-m', time()), 'is_total' => 2,'rwlx_id'=>1 ,'depar_id'=>3,'ygroup_id'=>$ygroup])->field('id,renwu,rwlx_id')->find();
        $cgps_renwu = $mbdatas['renwu'];
        $cgps_dayrenwu = round($cgps_renwu / 26);
        $mbdata['cgps_renwu'] = $type === 1?$cgps_dayrenwu:$cgps_renwu;
        $mbdata['cgps_all'] = $data['cgps']['all'];
        $mbdata['cgps_wcl'] = round(($mbdata['cgps_all'] / $mbdata['cgps_renwu']), 2) * 100;
        //成功预约
        $mbdatas = Db::name('renwus')->where(['status' => 1, 'date' => date('Y-m', time()), 'is_total' => 2,'rwlx_id'=>2 ,'depar_id'=>3,'ygroup_id'=>$ygroup])->field('id,renwu,rwlx_id')->find();
        $cgyy_renwu = $mbdatas['renwu'];
        $cgyy_dayrenwu = round($cgyy_renwu / 26);
        $mbdata['cgyy_renwu'] = $type === 1?$cgyy_dayrenwu:$cgyy_renwu;
        $mbdata['cgyy_all'] = $data['yymx']['all'];
        $mbdata['cgyy_wcl'] = round(($mbdata['cgyy_all'] / $mbdata['cgyy_renwu']), 2) * 100;
        //月目标差值
        if($type == 2){
            $day = (int)date('d',time());//求出今天是这个月的第几天
            $total_day = date('t');//当月有多少天
//            $mbdata['cgps_diff'] =  round(( ( $cgps_dayrenwu*$day-$mbdata['cgps_all'] ) / $cgps_renwu), 2) * 100;
//            $mbdata['cgyy_diff'] =  round(( ( $cgyy_dayrenwu*$day-$mbdata['cgyy_all'] ) / $cgyy_renwu), 2) * 100;
            $mbdata['cgps_diff'] =  round(( ( ($cgps_renwu/$total_day)*$day-$mbdata['cgps_all'] ) / $cgps_renwu), 2) * 100;
            $mbdata['cgyy_diff'] =  round(( ( ($cgyy_renwu/$total_day)*$day-$mbdata['cgyy_all'] ) / $cgyy_renwu), 2) * 100;
        }
        return $mbdata;
    }


    //今日、当月公共数据
//    public function get_Common_Data($data,$type){
//        $mbdata = $this->getMonthMb();
//        if( !empty($mbdata) ){
//            foreach ($mbdata as $k => $v) {
//                switch ( $v['rwlx_id'] ) {
//                    case 1://成功拍摄
//                        $mbdata[$k]['all'] = $data['cgps']['all'];
//                        $mbdata[$k]['detail'] = $data['cgps']['detail'];
//                        $mbdata[$k]['wcl'] = $this->get_Wcl_Data($mbdata[$k]['all'],$v['renwu'],$type);
//                        $mbdata[$k]['color'] = 'warning';
//                        break;
//                    case 2://成功预约
//                        $mbdata[$k]['all'] = $data['yymx']['all'];
//                        $mbdata[$k]['detail'] = '';
//                        $mbdata[$k]['wcl'] = $this->get_Wcl_Data($mbdata[$k]['all'],$v['renwu'],$type);
//                        $mbdata[$k]['color'] = 'success';
//                        break;
//                    case 3://反馈
//                        $mbdata[$k]['all'] = $data['fkkz']['all'];
//                        $mbdata[$k]['detail'] = $data['fkkz']['detail'];
//                        $mbdata[$k]['wcl'] = $this->get_Wcl_Data($mbdata[$k]['all'],$v['renwu'],$type);
//                        $mbdata[$k]['color'] = 'info';
//                        break;
//                    case 4://转介绍
//                        $mbdata[$k]['all'] = $data['zjs']['all'];
//                        $mbdata[$k]['detail'] = '';
//                        $mbdata[$k]['wcl'] = $this->get_Wcl_Data($mbdata[$k]['all'],$v['renwu'],$type);
//                        $mbdata[$k]['color'] = 'default';
//                        break;
//                }
//            }
//        }
//        return $mbdata;
//    }


    public function get_Common_Data($data,$type){
        //成功拍摄
        $mbdatas = Db::name('renwus')->where(['status' => 1, 'date' => date('Y-m', time()), 'is_total' => 1,'rwlx_id'=>1 ])->field('id,renwu,rwlx_id')->find();
        $cgps_renwu = $mbdatas['renwu'];
        $cgps_dayrenwu = round($cgps_renwu / 26);
        $mbdata['cgps_renwu'] = $type === 1?$cgps_dayrenwu:$cgps_renwu;
        $mbdata['cgps_all'] = $data['cgps']['all'];
        $mbdata['cgps_wcl'] = round(($mbdata['cgps_all'] / $mbdata['cgps_renwu']), 2) * 100;
        //成功预约
        $mbdatas = Db::name('renwus')->where(['status' => 1, 'date' => date('Y-m', time()), 'is_total' => 1,'rwlx_id'=>2 ])->field('id,renwu,rwlx_id')->find();
        $cgyy_renwu = $mbdatas['renwu'];
        $cgyy_dayrenwu = round($cgyy_renwu / 26);
        $mbdata['cgyy_renwu'] = $type === 1?$cgyy_dayrenwu:$cgyy_renwu;
        $mbdata['cgyy_all'] = $data['yymx']['all'];
        $mbdata['cgyy_wcl'] = round(($mbdata['cgyy_all'] / $mbdata['cgyy_renwu']), 2) * 100;
        //反馈
        $mbdatas = Db::name('renwus')->where(['status' => 1, 'date' => date('Y-m', time()), 'is_total' => 1,'rwlx_id'=>3 ])->field('id,renwu,rwlx_id')->find();
        $fkkz_renwu = $mbdatas['renwu'];
        $fkkz_dayrenwu = round($cgyy_renwu / 26);
        $mbdata['fkkz_renwu'] = $type === 1?$fkkz_dayrenwu:$fkkz_renwu;
        $mbdata['fkkz_all'] = $data['fkkz']['all'];
        $mbdata['fkkz_wcl'] = round(($mbdata['fkkz_all'] / $mbdata['fkkz_renwu']), 2) * 100;
        //转介绍
        $mbdatas = Db::name('renwus')->where(['status' => 1, 'date' => date('Y-m', time()), 'is_total' => 1,'rwlx_id'=>4 ])->field('id,renwu,rwlx_id')->find();
        $zjs_renwu = $mbdatas['renwu'];
        $zjs_dayrenwu = round($cgyy_renwu / 26);
        $mbdata['zjs_renwu'] = $type === 1?$zjs_dayrenwu:$zjs_renwu;
        $mbdata['zjs_all'] = $data['zjs']['all'];
        $mbdata['zjs_wcl'] = round(($mbdata['zjs_all'] / $mbdata['zjs_renwu']), 2) * 100;
        //月目标差值
        if($type == 2){
            $day = (int)date('d',time());//求出今天是这个月的第几天
            $total_day = date('t');//当月有多少天
//            $mbdata['cgps_diff'] =  round(( ( $cgps_dayrenwu*$day-$mbdata['cgps_all'] ) / $cgps_renwu), 2) * 100;
//            $mbdata['cgyy_diff'] =  round(( ( $cgyy_dayrenwu*$day-$mbdata['cgyy_all'] ) / $cgyy_renwu), 2) * 100;
//            $mbdata['fkkz_diff'] =  round(( ( $fkkz_dayrenwu*$day-$mbdata['fkkz_all'] ) / $fkkz_renwu), 2) * 100;
//            $mbdata['zjs_diff']  =  round(( ( $cgps_dayrenwu*$day-$mbdata['zjs_all'] )  / $cgps_renwu), 2) * 100;
            $mbdata['cgps_diff'] =  round(( ( ($cgps_renwu/$total_day)*$day-$mbdata['cgps_all'] ) / $cgps_renwu), 2) * 100;
            $mbdata['cgyy_diff'] =  round(( ( ($cgyy_renwu/$total_day)*$day-$mbdata['cgyy_all'] ) / $cgyy_renwu), 2) * 100;
            $mbdata['fkkz_diff'] =  round(( ( ($fkkz_renwu/$total_day)*$day-$mbdata['fkkz_all'] ) / $fkkz_renwu), 2) * 100;
            $mbdata['zjs_diff']  =  round(( ( ($cgps_renwu/$total_day)*$day-$mbdata['zjs_all'] )  / $cgps_renwu), 2) * 100;
        }
        return $mbdata;
    }

    //获取任务
//    public function get_Renwu($renwu,$type){
//        if($type == 1){
//            $renwu = round($renwu / 26);//今日任务
//        }
//        return $renwu;
//    }


//    public function get_Wcl_Data($all,$renwu,$type){
////        $total_day = date('t');//当月有多少天
//        $day = (int)date('d',time());//求出今天是这个月的第几天
//        $dayrenwu = round($renwu / 26);//今日任务
//        if($type == 1){//今日
//            $data['renwu'] = $dayrenwu;
//            $data['init'] = round(($all / $dayrenwu), 2) * 100;
//        }elseif($type == 2){//当月
//            $data['renwu'] = $renwu;
//            $data['init'] = round(($all / $renwu), 2) * 100;//拍摄进度
//            $data['all_diff'] = $dayrenwu*$day - $all;
//            $data['diff'] = round(($data['all_diff'] / $renwu), 2) * 100;
//        }
//        return $data;
//    }

    public function getMonthMb()
    {
        $date = date('Y-m', time());
        $mbdata = model('renwus')->where(['status' => 1, 'date' => $date, 'is_total' => 1])->select();
        return $mbdata;
    }

    public function yiyuan_Detail()
    {
        $type = request()->param('type');
        $title = request()->param('title');
        if($type == 1) {
            $time = Time::today();
        } else {
            $time = Time::month();
        }
        $datas = collect(Db::name('order')->where(['status' => 1, 'qd_depar' => 1, 'jdtime' => ['between', $time] ])->field('id,area,yiyuan')->select());
        $leibie = new Leibie();
        $area = $leibie->getArea();
        foreach ($area as $val) {
            $data[$val['id']]['area_name'] = $val['name'];
//            $data[$val['id']]['area_num'] = Db::name('order')->where(['status' => 1, 'qd_depar' => 1, 'jdtime' => ['between', $time], 'area' => $val['id']])->count();
            $data[$val['id']]['area_num'] = $datas->where('area',$val['id'])->count();
            $yiyuanInfo = $leibie->getYiyuan($val['id']);
            foreach ($yiyuanInfo as $v) {
                $data[$val['id']]['yiyuan'][$v['id']]['yiyuan_name'] = $v['name'];
//                $data[$val['id']]['yiyuan'][$v['id']]['yiyuan_num'] = Db::name('order')->where(['status' => 1, 'qd_depar' => 1, 'jdtime' => ['between', $time], 'yiyuan' => $v['id']])->count();
                $data[$val['id']]['yiyuan'][$v['id']]['yiyuan_num'] = $datas->where('yiyuan',$v['id'])->count();
            }
        }
        return view("yiyuan_detail", [ 'type' => $type,'data' => $data,'title'=>$title]);
    }

    //获取邀约分组
    public function getYgroup(){
        return Db::name('ygroup')->where(['status'=>1,'depar_id'=>3])->field('id,group_name')->select();
    }
    //获取邀约客服
    public function getYykf($ygroup_id = ''){
        if(empty($ygroup_id)){
            return Db::name('users')->where(['status'=>1,'depar_id'=>3])->field('id,username')->select();
        }else{
            return Db::name('users')->where(['status'=>1,'depar_id'=>3,'ygroup_id'=>$ygroup_id])->field('id,username')->select();
        }
    }


}